package com.diudiutechnology.www.yadiudiu.util.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.yadiudiu.model.UniversalModel;
import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.view.wheelview.AbstractWheelTextAdapter1;
import com.diudiutechnology.www.yadiudiu.util.view.wheelview.OnWheelChangedListener;
import com.diudiutechnology.www.yadiudiu.util.view.wheelview.WheelView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/12.
 */

public class UniversalPhotoPop extends PopupWindow implements View.OnClickListener {
    Context context;
    private WheelView wvProvince;
    private WheelView address_province;
    private WheelView wheelView;

    private View lyChangeAddress;
    private View lyChangeAddressChild;
    private TextView btnSure;
    private TextView btnCancel;
    private OnAddressCListener onAddressCListener;
    private String strProvince="";
    private String strCity = "";
    private String area="";
    List<UniversalModel> Model=new ArrayList<>();
    List<UniversalModel> UniversaModel=new ArrayList<>();
    List<UniversalModel> universaModel=new ArrayList<>();
    private AddressTextAdapter provinceAdapter;
    private  AddressTextAdapter  addressprovince;
    private  AddressTextAdapter  province;
    private int maxsize = 14;
    private int minsize = 12;
    int pos=0;//0、授信积分 1、干洗添加商品
    View view;
    public UniversalPhotoPop(final Context context, List<UniversalModel> Model,int pos) {
        super(context);
        this.context = context;
        this.Model=Model;
        this.pos=pos;

        Data();

    }
    public UniversalPhotoPop(final Context context, List<UniversalModel> Model, List<UniversalModel> UniversaModel,int pos) {
        super(context);
        this.context = context;
        this.Model=Model;
        this.UniversaModel=UniversaModel;

        this.pos=pos;

        Data();

    }
    public UniversalPhotoPop(final Context context, List<UniversalModel> Model, List<UniversalModel> UniversaModel,List<UniversalModel> universaModel,int pos) {
        super(context);
        this.context = context;
        this.Model=Model;
        this.UniversaModel=UniversaModel;
        this.universaModel=universaModel;
        this.pos=pos;

        Data();

    }
private void Data(){

    view = View.inflate(context, R.layout.photo_pop_merchants_type, null);
    wvProvince = (WheelView) view.findViewById(R.id.wv_address_province);
    address_province= (WheelView) view.findViewById(R.id.address_province);
    wheelView= (WheelView) view.findViewById(R.id.wheelView);
    lyChangeAddress = view.findViewById(R.id.ly_myinfo_changeaddress);
    lyChangeAddressChild = view.findViewById(R.id.ly_myinfo_changeaddress_child);
    btnSure = (TextView) view.findViewById(R.id.btn_myinfo_sure);
    btnCancel = (TextView) view.findViewById(R.id.btn_myinfo_cancel);
    if (pos==0){
        strProvince=Model.get(0).getYear();
    }else if (pos==1){
        strProvince=Model.get(0).getName();
    }
    //设置SelectPicPopupWindow的View
    this.setContentView(view);
    //设置SelectPicPopupWindow弹出窗体的宽
    this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    //设置SelectPicPopupWindow弹出窗体的高
    this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
    //设置SelectPicPopupWindow弹出窗体可点击
    this.setFocusable(true);
    //设置SelectPicPopupWindow弹出窗体动画效果
//		this.setAnimationStyle(R.style.AnimBottom);
    //实例化一个ColorDrawable颜色为半透明
    ColorDrawable dw = new ColorDrawable(0xb0000000);
    //设置SelectPicPopupWindow弹出窗体的背景
    this.setBackgroundDrawable(dw);
    lyChangeAddressChild.setOnClickListener(this);
    btnSure.setOnClickListener(this);
    btnCancel.setOnClickListener(this);
    provinceAdapter = new AddressTextAdapter(context, Model,0,maxsize, minsize);
    wvProvince.setVisibleItems(5);
    wvProvince.setViewAdapter(provinceAdapter);
    wvProvince.setCurrentItem(0);
    wvProvince.addChangingListener(new OnWheelChangedListener() {

        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            // TODO Auto-generated method stub
            String currentText = (String) provinceAdapter.getItemText(wheel.getCurrentItem());
            System.out.println("stringmd5result"+wheel.getCurrentItem()+currentText);
            strProvince = currentText;
            setTextviewSize(currentText, provinceAdapter);
        }
    });
    System.out.println("stringmd5result"+UniversaModel.size());
    if (UniversaModel.size()>0){
        if (pos==0){
            strCity=UniversaModel.get(0).getYear();
        }else if (pos==1){
            strCity=UniversaModel.get(0).getName();
        }
        address_province.setVisibility(View.VISIBLE);
        addressprovince = new AddressTextAdapter(context, UniversaModel,0,maxsize, minsize);
        address_province.setVisibleItems(5);
        address_province.setViewAdapter(addressprovince);
        address_province.setCurrentItem(0);
        address_province.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                // TODO Auto-generated method stub
                String currentText = (String) addressprovince.getItemText(wheel.getCurrentItem());
                System.out.println("stringmd5result"+wheel.getCurrentItem()+currentText);
                strCity = currentText;
                setTextviewSize(currentText, addressprovince);
            }
        });
    }else {
        address_province.setVisibility(View.GONE);
    }

    if (universaModel.size()>0){
        if (pos==0){
            area=universaModel.get(0).getYear();
        }else if (pos==1){
            area=universaModel.get(0).getName();
        }
        wheelView.setVisibility(View.VISIBLE);
        province = new AddressTextAdapter(context, universaModel,0,maxsize, minsize);
        wheelView.setVisibleItems(5);
        wheelView.setViewAdapter(province);
        wheelView.setCurrentItem(0);
        wheelView.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                // TODO Auto-generated method stub
                String currentText = (String) province.getItemText(wheel.getCurrentItem());
                System.out.println("stringmd5result"+wheel.getCurrentItem()+currentText);
                area = currentText;
                setTextviewSize(currentText, province);
            }
        });
    }else {
        wheelView.setVisibility(View.GONE);
    }
}
    private class AddressTextAdapter extends AbstractWheelTextAdapter1 {
        List<UniversalModel> list;

        protected AddressTextAdapter(Context context, List<UniversalModel> list, int currentItem,int maxsize, int minsize) {
            super(context, R.layout.item_birth_year, NO_RESOURCE,currentItem ,maxsize, minsize);
            this.list = list;
            setItemTextResource(R.id.tempValue);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
            return view;
        }

        @Override
        public int getItemsCount() {
            return list.size();
        }

        @Override
        protected CharSequence getItemText(int index) {
            String str="";
            if (pos==0){
                str = list.get(index).getYear();
            }else if (pos==1){
                str = list.get(index).getName();
            }
            return str;
        }

    }

    public void setAddresskListener(OnAddressCListener onAddressCListener) {
        this.onAddressCListener = onAddressCListener;
    }

    /**
     * 设置字体大小
     *
     * @param curriteItemText
     * @param adapter
     */
    public void setTextviewSize(String curriteItemText, AddressTextAdapter adapter) {
        ArrayList<View> arrayList = adapter.getTestViews();
        int size = arrayList.size();
        String currentText;
        for (int i = 0; i < size; i++) {
            TextView textvew = (TextView) arrayList.get(i);
            currentText = textvew.getText().toString();
            if (curriteItemText.equals(currentText)) {
                textvew.setTextSize(14);
            } else {
                textvew.setTextSize(12);
            }
        }
    }
    @Override
    public void onClick(View view) {
        if (view == btnSure) {

                if (onAddressCListener != null) {
                    onAddressCListener.onClick(strProvince,strCity,area);
                }


        } else if (view == btnCancel) {

        } else if (view == lyChangeAddressChild) {
            return;
        } else {
//			dismiss();
        }
        dismiss();
    }
    /**
     * 回调接口
     *
     * @author Administrator
     *
     */
    public interface OnAddressCListener {
        public void onClick(String province, String city, String area);
    }

}